package com.adnaan.backend.controller.wechat;

import cn.dev33.satoken.stp.StpUtil;
import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.Dishes;
import com.adnaan.backend.entity.Medicinal;
import com.adnaan.backend.entity.RecognizeRecord;
import com.adnaan.backend.service.DishesService;
import com.adnaan.backend.service.HerbalTabooService;
import com.adnaan.backend.service.MedicinalService;
import com.adnaan.backend.service.RecognizeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * author: adnaan
 * create: 2025-02-26 18:02
 * Description:
 */
@RestControllerMapping("/recognize")
@Tag(name = "识别", description = "识别相关")
public class RecognizeController {

    @Resource
    private MedicinalService medicinalService;

    @Resource
    private HerbalTabooService herbalTabooService;

    @Resource
    private RecognizeService recognizeService;

    @Resource
    private DishesService dishesService;

    @GetMapping("/result")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "获取药材信息")
    public Result<Medicinal> result(@RequestParam Long id) {
        Medicinal medicinal = medicinalService.getById(id);
        medicinal.setEfficacy(medicinalService.getEfficacy(id));
        medicinal.setPictures(medicinalService.pictures(id));
        medicinal.setTaboo(herbalTabooService.getById(medicinal.getTabooId()));
        return Result.success(medicinal);
    }

    /**
     * 保存识别记录
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "保存识别记录")
    public Result<Boolean> save(@RequestBody RecognizeRecord record) {
        record.setUserId(StpUtil.getLoginIdAsLong());
        if (recognizeService.save(record)) {
            return Result.success(true);
        }
        return Result.error();
    }

    /**
     * 获取识别记录
     */
    @GetMapping("/record")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "获取识别记录")
    public Result<Page<RecognizeRecord>> record(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        Page<RecognizeRecord> page = new Page<>(pageNo, pageSize);
        page = recognizeService.page(page, new QueryWrapper<RecognizeRecord>().eq("user_id", StpUtil.getLoginIdAsLong()));
        page.getRecords().forEach(item -> {
            if (item.getResult() != null) {
                Medicinal herbal = medicinalService.getById(item.getResult());
                item.setResultName(herbal.getName());
                item.setResultImage(herbal.getCover());
                item.setEfficacy(medicinalService.getEfficacy(item.getResult()));
                item.setTaboo(herbalTabooService.getById(herbal.getTabooId()));
            }
        });
        return Result.success(page);
    }

    /**
     * 删除单个识别记录
     */
    @GetMapping("/delete")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "删除单个识别记录")
    public Result<Boolean> delete(@RequestParam Long id) {
        if (recognizeService.removeById(id)) {
            return Result.success(true);
        }
        return Result.error();
    }

    @GetMapping("/deleteBatch")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "批量删除识别记录")
    public Result<Boolean> deleteBatch(@RequestParam String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());

        if (recognizeService.removeByIds(idList)) {
            return Result.success(true);
        }
        return Result.error();
    }

    @GetMapping("/dishes")
    @ApiOperationSupport(order = 6)
    @Operation(summary = "获取菜品推荐")
    public Result<List<Dishes>> dishes(@RequestParam Long herbalId) {
        // 根据药材ID查询相关的药膳
        List<Dishes> dishesList = dishesService.getDishesByHerbalId(herbalId);
        if (dishesList != null && !dishesList.isEmpty()) {
            // 遍历设置每个药膳的详细信息
            dishesList.forEach(dish -> {
                // 设置药膳的制作步骤
                dish.setSteps(dishesService.getStepsByDishId(dish.getId()));
                // 设置药膳所需的药材材料
                dish.setDosage(dishesService.getMaterialsByDishId(dish.getId()));
            });
            return Result.success(dishesList);
        }
        return Result.success(Collections.emptyList());
    }
}
